using UnityEngine;
using System.Collections;

public class DropManager : MonoBehaviour {
	
	private static DropManager Instance = null;
	private float RootingRange;
	public UILabel UI_Money;
	void Awake()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType( typeof( DropManager )) as DropManager;
			if( Instance != null )
			{
				Instance.InitDropManager();
			}
		}
		if( null == Instance )
		{
			GameObject DropInstance = new GameObject("DropManager");
			(DropInstance.AddComponent<DropManager>()).InitDropManager();;
		}		
	}
	public static DropManager getInstance()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType( typeof( DropManager )) as DropManager;
			if( Instance != null )
			{
				Instance.InitDropManager();
			}
		}
		if( null == Instance )
		{
			GameObject DropInstance = new GameObject("DropManager");
			(DropInstance.AddComponent<DropManager>()).InitDropManager();;
		}
		
		return Instance;
	}
	
	private ArrayList arrDropItem;
	
	void Update()
	{
		if(isTouchItem( GameManager.getInstance().Player_script ) )
		{
			RootingRange = GameManager.getInstance().Player_script.player_data_.Player_Rooting_Range ;
			for(int index = arrDropItem.Count - 1 ; index >= 0 ; index--)
			{
				if ( RootingRange >=  Vector3.Distance( (arrDropItem[index] as GameObject).transform.position, GameManager.getInstance().Player_script.transform.position) )
				{
					GameObject dummy = arrDropItem[index] as GameObject;
					arrDropItem.RemoveAt( index );
					GameManager.getInstance().All_game_data.Player_Gold += 100;
					print(GameManager.getInstance().All_game_data.Player_Gold);
					UI_Money.text = GameManager.getInstance().All_game_data.Player_Gold.ToString();
					Destroy(dummy);
				}
			}
		}
	}
	private void InitDropManager()
	{
		arrDropItem = new ArrayList();
		
	}
	public bool isTouchItem(Player _player_script)
	{
		if( arrDropItem.Count.Equals(0) )
			return false;
		foreach( GameObject itertor in arrDropItem )
		{
			if ( _player_script.player_data_.Player_Rooting_Range 
				>=  Vector3.Distance( itertor.transform.position, _player_script.transform.position) )
			{
				return true;
			}
		}
		return false;
	}
	public void CreateItem(Vector3 _pos,string _monster_name)
	{
		GameObject Item = Instantiate( Resources.Load("testItem") ) as GameObject;
		arrDropItem.Add(Item);
		Item.transform.position = _pos;
	}
}
